//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.system.logs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

/**
 * AdminLogsAction <br>
 * Action de administracion de logs
 * 
 * @author Aji
 */
public class AdminLogsAction extends AbstractBaseAction {

    private static final String CLASS_NAME = AdminLogsAction.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);

    /**
     * Descargar logs en formato .ZIP
     */
	public ActionForward downloadLogs(FordActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, WebSession websession, ActionMessages msgs, ActionMessages warns, ActionMessages errormsgs) {
		String METHOD_NAME = "downloadLogs";

		AdminLogsFacade admlogs = (AdminLogsFacade)FacadeProvider.getFacade(request, AdminLogsFacade.class);
				
		log.entering(CLASS_NAME, METHOD_NAME, websession.getUser().getId());

		admlogs.getZippedLogs(response);
		
		log.exiting(CLASS_NAME, METHOD_NAME);
		
		return null;
	}

    /**
     * Cambiar nivel de log de una clase
     */
	public ActionForward changeLogLevel(FordActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, WebSession websession, ActionMessages msgs, ActionMessages warns, ActionMessages errormsgs) {
		String METHOD_NAME = "changeLogLevel";
		log.entering(CLASS_NAME, METHOD_NAME, websession.getUser().getId());
        ActionForward result = mapping.findForward("success");

        if (this.isTokenValid(request, mapping)) {
            AdminLogsForm logsForm = (AdminLogsForm)form;
            AdminLogsFacade admlogs = (AdminLogsFacade)FacadeProvider.getFacade(request, AdminLogsFacade.class);
            
            String classSelected = logsForm.getClassSelected(),
                   levelSelected = logsForm.getLevel();
            log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "Class selected", classSelected);
            log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "Level selected", levelSelected);
            admlogs.changeLoggerLevel(classSelected, levelSelected);
            log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "Log Level changed");

            msgs.add(Globals.MESSAGE_KEY, new ActionMessage("admin_change_log_level", classSelected, levelSelected));
    
        }
        else{
            log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "Token not valid");
            result = mapping.findForward("error_token");
        }
		
		log.exiting(CLASS_NAME, METHOD_NAME);
		return result;
	}
    
	/**
	 * @see ar.com.ford.it.webengine.struts.helper.AbstractBaseAction#initializer(FordActionMapping, ActionForm, HttpServletRequest, HttpServletResponse, WebSession)
	 */
	public ActionForward initPage(FordActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, WebSession websession, ActionMessages msgs, ActionMessages warns, ActionMessages errormsgs) {
		
		AdminLogsForm pageForm = (AdminLogsForm)form;
		AdminLogsFacade admlogs = (AdminLogsFacade)FacadeProvider.getFacade(request, AdminLogsFacade.class);
		
		pageForm.setJavaPackages(admlogs.getApplicationPackages());
		
		return mapping.findForward("success");
	}
    
    /**
     * Llenar la collection de clases de acuerdo al package seleccionado
     */
	public ActionForward getClassesFromPackage(FordActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, WebSession websession, ActionMessages msgs, ActionMessages warns, ActionMessages errormsgs) {
		
		AdminLogsForm pageForm = (AdminLogsForm)form;
		AdminLogsFacade admlogs = (AdminLogsFacade)FacadeProvider.getFacade(request, AdminLogsFacade.class);
		
        if(!"".equals(pageForm.getPackSelected())) {
    		pageForm.setJavaClasses(admlogs.getClassesFromPackage(pageForm.getPackSelected()));
        }
        else{
            pageForm.setJavaClasses(null);
        }

		
		return mapping.findForward("success");
	}    
    
}
